﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Sita.Data.Context;
using Sita.Entities;

namespace Sita.Data.Repositories
{
    public class ModuleRepository : GenericRepository<Module>
    {
        public ModuleRepository()
            : base(new SitaContext())
        {

        }

        public IList<Module> GetModulesWithPermissions()
        {
            SitaContext _context = new SitaContext();
            IList<Module> Modules;

            Modules = _context.Modules.Include("Permissions").ToList();

            return Modules;
        }

        public IList<Module> GetModulesWithUserPermissionsByUserID(int userID)
        {
            SitaContext _context = new SitaContext();
            IList<Module> Modules;

            Modules = (from m in _context.Modules
                       join p in _context.Permissions on m.ModuleID equals p.ModuleID
                       join u in _context.UserPermissions on p.PermissionID equals u.PermissionID
                       where u.UserID.Equals(userID) && p.Access.Equals(true)
                       select m).Distinct().OrderBy(m => m.Position).ToList();
            return Modules;
        }

        public IList<Module> GetWithPermissionsByBusinessUnitID(int buID)
        {
            SitaContext _context = new SitaContext();
            IList<Module> Modules;
            IList<BusinessUnitPermission> BUPermissions;
            bool FoundResource;

            Modules =
                _context.Modules.Include("Permissions").ToList();
            BUPermissions = _context.BusinessUnitPermissions.Where(bu => bu.BusinessUnitID == buID).ToList();

            for (int i = 0; i < Modules.Count; i++)
            {
                for (int j = 0; j < Modules[i].Permissions.Count; j++)
                {
                    FoundResource = false;
                    foreach (var BUPermission in BUPermissions)
                    {
                        if (Modules[i].Permissions[j].PermissionID == BUPermission.PermissionID)
                        {
                            FoundResource = true;
                            break;
                        }
                    }

                    if (!FoundResource)
                    {
                        Modules[i].Permissions.RemoveAt(j);
                        j--;
                    }
                }

                if (Modules[i].Permissions.Count == 0)
                {
                    Modules.RemoveAt(i);
                    i--;
                }
            }


            return Modules;
        }

        public Module GetByIDWithBusiness(int moduleID)
        {
            SitaContext _context = new SitaContext();
            Module FoundModule;


            FoundModule = _context.Modules.Include("ModuleBusinesses").Where(m => m.ModuleID == moduleID).SingleOrDefault();

            return FoundModule;
        }
    }
}
